// pages/detail/detail.js
import IndexModel from '../../models/IndexModel';
import Config from '../../utils/config';
const indexModel = new IndexModel();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    sel: '1', // footer-bar sel
    isFavor: false,
    type: '2', // type - 2 zixun 3 jinnang
    id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('detail->', options);
    wx.showLoading({
      title: '加载中',
    })
    if (options.tableName === 'jyJinnang') {
      indexModel.getPackageDetail(options.id).then(res => {
        // console.log('package detail ->', res);
        this.setData({
          detail: res.ListJson.List[0],
          sel: Config.tableName[options.tableName],
          type: '3',
          id: options.id,
        })
        this.judgeIsFavor(this.data.id, this.data.type);
        wx.hideLoading();
      })
    } else {
      indexModel.getIndexInfo(options.id).then(res => {
        // console.log('res->', res);
        this.setData({
          detail: res.ListJson.List[0],
          sel: Config.tableName[options.tableName],
          type: '2',
          id: options.id,
        })
        this.judgeIsFavor(this.data.id, this.data.type);
        wx.hideLoading();
      })      
    }
  },

  clickFavor(e) {
    let sel = e.detail.sel;
    let {
      id,
      type
    } = this.data;
    indexModel.favor('1', type, id, sel, app.globalData.userId).then(res => {
      // console.log('favor22222->', res);
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 2000,
      })
    })
  },

  judgeIsFavor(id, type) {
    indexModel.judgeFavor(id, type, app.globalData.userId).then(res => {
      // console.log('favor1111 ->', res);
      let isFavor = res && res.IsMark === '1';
      this.setData({
        isFavor,
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})